﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  User
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using OpenTwitter.SDK.Http;
using OpenTwitter.SDK.Sina.Objects;

namespace OpenTwitter.SDK.Sina.API
{
    /// <summary>
    /// 与帐户相关的接口实现
    /// </summary>
    public class User
        : APIRequest
    {
        /// <summary>
        /// 根据授权对象实例化
        /// </summary>
        /// <param name="oauth"></param>
        public User(OAuth oauth)
            : base(oauth)
        { }

        #region 获取自己的资料
        /// <summary>
        /// 采用默认API请求地址获取用户本人帐号信息
        /// </summary>
        /// <returns>本人帐号信息.</returns>
        public UserProfileData GetProfile()
        {
            return this.GetProfile("http://api.t.sina.com.cn/account/verify_credentials");
        }
        /// <summary>
        /// 获取用户本人帐号信息
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <returns>本人帐号信息.</returns>
        public UserProfileData GetProfile(string requestUrl)
        {
            this.Clear();
            return this.GetResponseData<UserProfileData>(requestUrl);
        }
        #endregion

        #region 更新用户头像信息
        /// <summary>
        /// 采用默认API请求地址更新用户头像信息
        /// </summary>
        /// <param name="filePath">本址头像图片文件地址</param>
        /// <returns>用户个人资料</returns>
        public UserProfileData UpdateHead(string filePath)
        {
            return this.UpdateHead(new UploadFile(filePath));
        }
        /// <summary>
        /// 采用默认API请求地址更新用户头像信息
        /// </summary>
        /// <param name="headPicture">头像图片文件</param>
        /// <returns>更新结果</returns>
        public UserProfileData UpdateHead(UploadFile headPicture)
        {
            return this.UpdateHead("http://api.t.sina.com.cn/account/update_profile_image", headPicture);
        }
        /// <summary>
        /// 更新用户头像信息
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="headPicture">头像图片文件</param>
        /// <returns>用户个人资料</returns>
        public UserProfileData UpdateHead(string requestUrl, UploadFile headPicture)
        {
            this.Clear();
            this.Files.Add("image", headPicture);

            return this.GetResponseData<UserProfileData>(requestUrl);
        }
        #endregion

        #region 更新用户信息
        /// <summary>
        /// 采用默认API请求地址更新用户信息
        /// </summary>
        /// <param name="name">昵称， 如果不更改请置为null值</param>
        /// <param name="gender">性别， 如果不更改请置为Unknown值</param>
        /// <param name="province">地区码， 如果不更改请置为0</param>
        /// <param name="city">城市码， 如果不更改请置为0</param>
        /// <param name="description">个人介绍，不超过160个汉字， 如果不更改请置为null值</param>
        /// <returns>用户个人资料</returns>
        public UserProfileData Update(string name, Gender gender, int province, int city, string description)
        {
            return this.Update("http://api.t.sina.com.cn/account/update_profile", name, gender, province, city, description);
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="name">昵称， 如果不更改请置为null值</param>
        /// <param name="gender">性别， 如果不更改请置为Unknown值</param>
        /// <param name="province">地区码， 如果不更改请置为0</param>
        /// <param name="city">城市码， 如果不更改请置为0</param>
        /// <param name="description">个人介绍，不超过160个汉字， 如果不更改请置为null值</param>
        /// <returns>用户个人资料</returns>
        public UserProfileData Update(string requestUrl, string name, Gender gender, int province, int city, string description)
        {
            this.Clear();
            if (name != null) this.Parameters.Add("name", name);
            if (gender != Gender.Unknown) this.Parameters.Add("sex", gender == Gender.Female ? "f" : "m");
            if (province > 0) this.Parameters.Add("province", province);
            if (city > 0) this.Parameters.Add("city", city);
            if (description != null) this.Parameters.Add("description", description);
            return this.GetResponseData<UserProfileData>(RequestMethod.POST, requestUrl);
        }
        #endregion

        #region 获取其他人资料
        /// <summary>
        /// 采用默认API请求地址获取其他人资料
        /// </summary>
        /// <param name="uid">他人的UID</param>
        /// <returns>帐号信息.</returns>
        public UserProfileData GetOtherProfile(long uid)
        {
            return GetOtherProfile(uid, null);
        }
        /// <summary>
        /// 采用默认API请求地址获取其他人资料
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        /// <returns>帐号信息.</returns>
        public UserProfileData GetOtherProfile(string name)
        {
            return GetOtherProfile(0, name);
        }
        /// <summary>
        /// 采用默认API请求地址获取其他人资料
        /// </summary>
        /// <param name="uid">他人的UID</param>
        /// <param name="name">他人的帐户名</param>
        /// <returns>帐号信息.</returns>
        public UserProfileData GetOtherProfile(long uid, string name)
        {
            return this.GetOtherProfile("http://api.t.sina.com.cn/users/show", uid, name);
        }
        /// <summary>
        /// 获取其他人资料
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="uid">他人的UID</param>
        /// <param name="name">他人的帐户名</param>
        /// <returns>帐号信息.</returns>
        public UserProfileData GetOtherProfile(string requestUrl, long uid, string name)
        {
            this.Clear();
            if (uid > 0) this.Parameters.Add("user_id ", uid);
            if (!string.IsNullOrEmpty(name)) this.Parameters.Add("screen_name", name);
            return this.GetResponseData<UserProfileData>(requestUrl);
        }
        #endregion
    }
}
